package com.future.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.model.po.SecondFillBlankModel;

public class SecondFillBlankDAO extends BaseDAO<SecondFillBlankModel> {
	private static Logger logger = LogManager.getLogger(SecondFillBlankDAO.class);

	public SecondFillBlankDAO() {

	}

	public SecondFillBlankDAO(Connection conn) {
		super(conn);
	}

	public boolean save(SecondFillBlankModel model) {
		String sql = "insert into secondfillblank (userid,item0101,item0102,item0201,item0202,item0203,item0204,item0205,item0206,item0301,item0302,item0401,item0402) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		logger.info("sql="+sql);
		return this.update(sql, model.getUserid(), model.getItem0101(), model.getItem0102(), model.getItem0201(),
				model.getItem0202(), model.getItem0203(), model.getItem0204(), model.getItem0205(), model.getItem0206(),
				model.getItem0301(), model.getItem0302(), model.getItem0401(), model.getItem0402());

	}
	public boolean update(SecondFillBlankModel model) {
		String sql = "update secondfillblank set item0101=?,item0102=?,item0201=?,item0202=?,item0203=?,item0204=?,item0205=?,item0206=?,item0301=?,item0302=?,item0401=?,item0402=?  where userid=?";
		logger.info("updateSecondFillBlankModelSql = "+ sql);
		return this.update(sql, model.getItem0101(), model.getItem0102(), model.getItem0201(),
				model.getItem0202(), model.getItem0203(), model.getItem0204(), model.getItem0205(), model.getItem0206(),
				model.getItem0301(), model.getItem0302(), model.getItem0401(), model.getItem0402(),model.getUserid());
		
	}

	public SecondFillBlankModel findByUserId(Integer userId) throws Exception {
		SecondFillBlankModel model = new SecondFillBlankModel();
		model.setUserid(userId);
		return this.findByUserId(model);
	}

	public SecondFillBlankModel findByUserId(SecondFillBlankModel model) throws Exception {
		String sql = "select * from secondfillblank where userid = ?";
		List<SecondFillBlankModel> modelList = this.searchAsBeanList(sql, SecondFillBlankModel.class, model.getUserid());
		int modelCount = modelList.size();
		if (modelCount != 1) {
			throw new Exception("获取部门调查填空问题信息出错");
		}
		return modelList.get(0);
	}
	
	/**
	 * 判断当前登录用户是否做过该调查
	 * @param model
	 * @return true  表示做过  false  表示没做过
	 */
	public boolean isOrExist(SecondFillBlankModel model) {
		String sql = "select * from secondfillblank where userid = ?";
		logger.info("sql="+sql);
		List<SecondFillBlankModel> list = this.searchAsBeanList(sql, SecondFillBlankModel.class, model.getUserid());
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public List<SecondFillBlankModel> find(Map<String, Object> condition) {

		StringBuffer sb = new StringBuffer("select * from secondfillblank where ");
		List<Object> values = new ArrayList<Object>();
		Iterator<String> columns = condition.keySet().iterator();
		while (columns.hasNext()) {
			String column = columns.next();
			sb.append(column + " = ? ");
			values.add(condition.get(column));
			if (columns.hasNext()) {
				sb.append(" and ");
			}
		}
		Object[] params = values.toArray();
		String sql = sb.toString();
		logger.debug(sql);
		logger.debug(params);
		return this.searchAsBeanList(sql, SecondFillBlankModel.class, params);
	}
	/**
	 * 地方调查统计
	 * petter 20140224
	 * @return
	 * @throws Exception 
	 */
	public List<Map<String, Object>>  getStatistical(Integer id){
		StringBuffer sql = new StringBuffer();
		sql.append("  select sum(item0101) item0101,sum(item0102) item0102,(select count(item0201) from secondfillblank sf   ")
		.append("   INNER JOIN user u on u.id=sf.userid where u.departmentid between 2 and 33 and u.areaid = ? and item0201=1 ) item0201 ,   ")
		.append(" sum(item0202) item0202,sum(item0203) item0203,SUM(item0204) item0204,SUM(item0205) item0205, ")
		.append(" SUM(item0206) item0206,SUM(item0301) item0301,SUM(item0302) item0302 from secondfillblank sf  ")
		.append("  INNER JOIN user u on u.id=sf.userid where u.departmentid between 2 and 33 and u.areaid = ?  ");
		logger.info("sql= "+sql.toString());
		return this.searchAsMapList(sql.toString(), id,id);
	}
}
